package com.gjy.quartz.ctl;

import com.gjy.quartz.job.DownloadFileJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * @author gjy
 * @version 1.0
 * @since 2024-11-29 11:09:55
 */
public class DownloadFileJobCtl {
    private static final Logger log = LoggerFactory.getLogger(DownloadFileJobCtl.class);

    public static void run(String jobName, String jobGroup, String triggerName, String triggerGroup) throws SchedulerException {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();
        try {
            String uuid = UUID.randomUUID().toString();
            log.info("发送: {}", uuid);
            JobDetail jobDetail = JobBuilder.newJob(DownloadFileJob.class).usingJobData("taskId", uuid)
                    .withIdentity(jobName, jobGroup).build();
            String cron = "*/30 * * * * ?";
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        scheduler.start();
    }

}
